package com.owg.jigsaw.blocktypes;



import java.awt.Component;
import java.awt.Label;

import com.owg.jigsaw.WBlock;
import com.owg.jigsaw.WBlockBase;
import com.owg.jigsaw.WRoot;
import com.owg.jigsaw.javafx.FXTextField;

import owg.util.data.property.Property;
import owg.util.data.property.PropertyListener;
import javafx.scene.Group;
import javafx.scene.paint.Color;

/**
 * A label block. The label block has a text field and no slots, and produces an AWT label.
 */
public class WLabelBlock extends WBlockBase implements WComponentBlock
{
	public final Property<String> title = new Property<String>("sample text");
	
	private FXTextField titleField;
	
	
	public WLabelBlock(WRoot root, boolean isPaletteBlock, String title)
	{
		super(root, Label.class, isPaletteBlock, Color.LIMEGREEN);
		this.title.set(title);
		titleField = new FXTextField(title);
		//Mutual binding
		PropertyListener<? super String> fieldUpdater = (prop, oldValue, newValue) -> {titleField.setText(newValue);};
		this.title.addListener(referencer, fieldUpdater);
		titleField.onActionProperty().set(e -> {WLabelBlock.this.title.set(titleField.getText(), fieldUpdater);});
		//Install
		getCell(0,0).installContentPane(new Group(titleField));
	}
	@Override
	public WBlock copy(boolean withChildren)
	{
		return new WLabelBlock(root, false, title.get());
	}
	@Override
	public Component getComponent()
	{
		return new Label(title.get());
	}
	
	@Override
	public String toString()
	{
		return "Label: "+title.get();
	}
}
